﻿using System.Collections.Generic;
using System.IO;
using System.Web.Hosting;
using System.Xml;

namespace visinia.SmartEngine.Installer
{
    public class ThemeInstaller
    {
        internal static bool HandleTheme(string viFile, List<ContentFileInfo> ContentFileList, XmlDocument doc, XmlNode typeNode)
        {
            XmlNode dataNode = typeNode["data"];
            if (dataNode != null)
            {
                // the theme must have a name;
                XmlNode nameNode = dataNode["name"];
                if (nameNode == null)
                    return false;
                string themeName = nameNode.InnerText;
                if (string.IsNullOrEmpty(themeName))
                    return false;

                // if this theme is not specified in content file don't install it either
                ContentFileInfo cfInfo = ContentFileList.Find
                    (p => ((p.Type == ContentFileType.Theme) &&
                           (string.Compare(p.Name, themeName, true) == 0) &&
                           (p.IsInstalled == false)));

                if (cfInfo == null)
                    return false;
                // this theme is already installed
                if (cfInfo.IsInstalled)
                    return false;

                XmlNode filesNode = typeNode["files"];
                if (filesNode != null)
                {
                    string directory = Path.GetDirectoryName(viFile);
                    foreach (XmlNode fileNode in filesNode.ChildNodes)
                    {       
                        if (string.Compare(fileNode.Name, "themes", true) == 0)
                        {
                            string sourcePath = Path.Combine(directory, fileNode.InnerText);
                            if (!File.Exists(sourcePath))
                                continue;
                            // copy files to the site themes folder
                            string root = HostingEnvironment.MapPath(WebRoots.GetThemesRoot);
                            CopyFile(themeName, fileNode, sourcePath, root);
                        }
                    }
                }
            }
            return true;
        }

        private static void CopyFile(string themeName, XmlNode fileNode, string sourcePath, string root)
        {
            string directoryName = root + themeName;
            string destinationPath = Path.Combine(directoryName, fileNode.InnerText);
            string dirName = Path.GetDirectoryName(destinationPath);
            if (dirName.Length > 0)
                Directory.CreateDirectory(dirName);
            // overwrite file if already exists
            File.Copy(sourcePath, destinationPath, true);
        }

        #region Uninstaller
        internal static bool UnInstallTheme(string ThemeName)
        {
            // is this theme already installed
            string themePath = HostingEnvironment.MapPath(WebRoots.GetThemesRoot + ThemeName + "\\page.master");
            if( ! File.Exists(themePath))
                return false;
            // just delete every thing
            Directory.Delete(Path.GetDirectoryName(themePath), true);
            return true;
        }
        #endregion        
    }
}